using System;
using Lily.TestHelper.Configs;
using Lily.TestHelper.Exceptions;
using Lily.TestHelper.UI;
using White.Core;
using White.Core.Configuration;
using White.Core.UIItems;

namespace Lily.TestHelper.White
{
	public class WhiteApplicationDriver : IWinformApplicationDriver
	{
		private readonly Application _application;

		public WhiteApplicationDriver(String pathToExe)
		{
			var config = CoreAppXmlConfiguration.Instance;
			config.BusyTimeout = MainConfig.WaitTimeout;
			_application = Application.Launch(pathToExe);
		}

		public IWindowDriver SelectWindowDriver(string formName)
		{
			try
			{
				var window = _application.GetWindow(formName);
				return new WhiteWindowDriver(window);
			}
			catch (UIActionException uiActionException)
			{
				throw new WindowNotFoundException(uiActionException);
			}
		}

		public void Dispose()
		{
			_application.Dispose();
		}
	}
}